package messages;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.Statement;
import ocsf.server.ConnectionToClient;
import controllers.DBConnection;
import controllers.WorkerController;
import controllers.CustomerController;
import entity.User;
import entity.UserLoginInfo;
import controllers.EchoServer;;

public class LoginMessage 
{
	String[] userArr;
	ConnectionToClient client;
	WorkerController workerC;
	CustomerController customerC;
	UserLoginInfo loginInfo;
	//EchoServer echo;
	
	User user;
	private String userID;
	private String password;
	private int status;
	private int loginResult = -9;
	private int permmision = 0 ;
	private String Fname ; 
	private String Lname;
	private String email;
	private String phone;

	public LoginMessage() {}
	
	
	public LoginMessage(String[] user, ConnectionToClient client)
	{
		this.userArr=user;
		this.client=client;	
	}
	
	
	public void activation(){
		try{
			Statement stmt;
			DBConnection conn=new DBConnection();
			java.sql.PreparedStatement pr1= conn.getDBConnection().prepareStatement("SELECT * " + "FROM sgi_system.users " + "WHERE ID ='" + userArr[1]+"' AND Password='" + userArr[2]+"';");
			ResultSet rs=pr1.executeQuery();			
			
			if(!rs.next()){
				pr1.close();
				rs.close();
				System.out.println("No match between id and password that inserted in DB");
			}// No match id and password
			else if( 1==(rs.getInt("Status") )) {
				pr1.close();
				rs.close();
				System.out.println("user whit this id already loged");
			}//user already connected
			else{
				    //setting user data 		 	
				 	 this.setStatus(rs.getInt("Status"));
                     this.setUser(rs.getString("ID"));
                     this.setPass(rs.getString("Password"));
                     this.setLoginPermmision(rs.getInt("Permmision"));
                     this.setEmail(rs.getString("email"));
                     this.setFname(rs.getString("FirstName"));
                     this.setLname(rs.getString("LastName"));
                     this.setPhone(rs.getString("PhoneNumber"));
                     
                     //user info as written in DB
                     user=new User(userID,password, Fname,Lname,email,phone,permmision,status);
                     user.printUser();
                  
                     //update DB of user connection	
                     DBConnection conn2= new DBConnection();
                     java.sql.PreparedStatement pr2 = conn2.getDBConnection().prepareStatement( "UPDATE `sgi_system`.`users` " + "SET `Status`='1' " +  "WHERE `ID`="+"'"+userArr[1]+"'"+";");
                     pr2.executeUpdate();
               
					if(this.permmision==1 ){
						customerC = new CustomerController(user);
						customerC.activateGUI();
					}
                     else {
                    	 workerC = new WorkerController(user);
                    	 workerC.activateGUI();
                     }
                    	 
             }
		}catch (Exception e)
	       {
	    	   e.printStackTrace();
	       
	       }
		
	}

	
	
	
	
	public String getUser()
	{
		return this.userID;
	}
	public void setUser(String id)
	{
		 this.userID = id;
	}

	public String getPassword()
	{
		return this.password;
	}
	
	public void setPass(String pass)
	{
		 this.password = pass;
	}
	
    public int getLoginResult()
    {
    	return this.loginResult;
    }
	
    public void setLoginPermmision(int permmision)
    {
    	this.permmision=permmision; 
    }
	
    public int getLoginPermmision()
    {
    	return this.permmision;
    }
	
    public void setLoginResult(int result)
    {
    	this.loginResult = result ; 
    }

	public String getFname() 
	{
		return this.Fname;
	}

	public void setFname(String fname) 
	{
		this.Fname = fname;
	}

	public String getLname()
	{
		return this.Lname;
	}

	public void setLname(String lname) 
	{
		this.Lname = lname;
	}

	public int getStatus()
	{
		return this.status;
	}

	public void setStatus(int status)
	{
		this.status = status;
	}


	public String getEmail()
	{
		return this.email;
	}

	public void setEmail(String email) 
	{
		this.email = email;
	}

	public String getPhone()
	{
		return this.phone;
	}

	public void setPhone(String phone) 
	{
		this.phone = phone;
	}
    
	public ConnectionToClient getClient()
	{
		return this.client;
	}

	
}
	

